import React, { FC, useEffect } from 'react';
import { Modal, Form, InputNumber, Input, DatePicker} from 'antd';
import { PersonSalaryItem } from '@/services/person/salary';

interface OperationModalProps {
  open: boolean;
  personSalaryItem: Partial<PersonSalaryItem> | undefined;
  onSubmit: (values: PersonSalaryItem) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const PersonSalaryForm: FC<OperationModalProps> = (props) => {
  const [form] = Form.useForm();
  const { open, personSalaryItem, onCancel, onSubmit } = props;

  useEffect(() => {
    if (form && !open) {
      form.resetFields();
    }
  }, [props.open]);

  const handleSubmit = () => {
    if (!form){
      return;
    } 
    form.submit();
  };

  const handleFinish = (values: { [key: string]: any }) => {
    if (onSubmit) {
      onSubmit({ ...values } as PersonSalaryItem);
    }
  };

  const modalFooter = { okText: '保存', onOk: handleSubmit, onCancel };

  const renderContent = () => {
    return (
      <>
        <Form.Item
          label="日期"
          name="salaryDate"
          rules={[{ required: true, message: '请选择日期' }]}
        >
          <DatePicker style={{ width: '100%' }} picker="date" placeholder="请选择日期" />
        </Form.Item>
        <Form.Item
          label="基本工资"
          name="baseIn"
          rules={[{ required: true, message: '请输入基本工资' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入基本工资"
          />
        </Form.Item>
        <Form.Item
          label="节日费"
          name="festivalFee"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入节日费"
          />
        </Form.Item>
        <Form.Item
          label="福利费"
          name="welfare"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入福利费"
          />
        </Form.Item>
        <Form.Item
          label="其他费"
          name="otherIn"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入其他费"
          />
        </Form.Item>
        <Form.Item
          label="公积金"
          name="accumulationFund"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入公积金"
          />
        </Form.Item>
        <Form.Item
          label="社保金额"
          name="socialSecurity"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入社保金额"
          />
        </Form.Item>
        <Form.Item
          label="其他扣除"
          name="otherOut"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入其他扣除"
          />
        </Form.Item>
        <Form.Item
          label="个税金额"
          name="tax"
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入个税金额"
          />
        </Form.Item>
      </>
    );
  };

  return (
    <Modal
      title= {'添加工资'}
      width={640}
      destroyOnClose
      open={open}
      {...modalFooter}
    >
      <Form {...formLayout} form={form} onFinish={handleFinish}>
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default PersonSalaryForm;
